package com.example.demo.practice.area;

import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.demo.practice.R;
import com.example.demo.practice.innerclass.IBaseActivity;
import com.example.demo.practice.monitoring.WaterLevelList;
import com.example.demo.practice.tool.ServerHttp;
import com.example.demo.practice.ui.MapAndList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OtherDivision extends IBaseActivity {

    private MapAndList mapAndList;
    private List<String> coordinates;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            JSONArray jsonArray = serverHttp.getData();
            coordinates = new ArrayList<String>();
            List<String> array = new ArrayList<String>();
            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject json = null;
                try {
                    json = (JSONObject) jsonArray.get(i);
                    array.add(json.getString("gid")+ "区");
                    coordinates.add(json.getString("geom"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            mapAndList.setArrayAdapter(new ArrayAdapter<String>(OtherDivision.this,R.layout.list_item_simple,array));
            mapAndList.setItemClickListener(new SingleClick());
            serverHttp.clear();
            ListView listView =mapAndList.getExpandableListView();
            listView.setDivider(new ColorDrawable(getResources().getColor(R.color.LineView)));
            listView.setDividerHeight(1);
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_division);
        setTitle("其他划分");
        mapAndList = (MapAndList) findViewById(R.id.mapAndList);
        mapAndList.loadUrl("html/area/OtherDivision.html");
        serverHttp = new ServerHttp(handler);
        serverHttp.setUrl("otherdivision");
    }

    public class SingleClick implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String geom=String.valueOf(coordinates.get(i));
            int s1=geom.indexOf("[");
            int s2=geom.indexOf("]");
            String coordinates=geom.substring(s1+1,s2);
            String[] latLng=coordinates.split(",");
            double lng= Double.parseDouble(latLng[0]);
            double lat= Double.parseDouble(latLng[1]);
            mapAndList.setZoomCenter(lat,lng,11);
        }
    }
}
